using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using XNAProjectLibrary;


namespace XNAProject.GameElements
{
    public class ConsoleMenu : Microsoft.Xna.Framework.DrawableGameComponent                                //menubar on playscene
    {
        private SpriteFont font;
        private SpriteFont timerFont;   
        private XNAGame OurGame;

        private Texture2D resources;
        private Texture2D panelRight;
        private Texture2D panelLeft;
        private Texture2D actionBackground;
        private Texture2D timerBackground;
        private Texture2D timerBackgroundNetural;
        private Texture2D timerBackgroundOn;
        private Texture2D timerBackgroundOff;
        private Rectangle timerRectangle;

        private float resourcesScale;
        private float panelScale;
        private float actionScale;
        private float timerScale;

        private string panelLeftName;
        private string panelRightName;
        private string panelLeftText;
        private string panelRightText;
        private string actionText;
        private string timerText;

        private float fadeHelper = 0.0f;
        private float fadeText = 255;
        private bool action = false;

        private Vector2 resourcePosition;
        private Vector2 panelRightPosition;
        private Vector2 panelLeftPosition;
        private Vector2 actionPosition;
        private Vector2 timerBackgroundPosition;
        private Vector2 timerPosition;

        private IInputHandler Input;

        public ConsoleMenu(Game game)
            : base(game)
        {
            OurGame = (XNAGame)game;
            GraphicsDeviceManager graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));
            Input = (IInputHandler)Game.Services.GetService(typeof(IInputHandler));

                                //set up post/scale
            resourcesScale = 3.6f * graphics.PreferredBackBufferWidth / 10000;
            timerScale = 3.6f * graphics.PreferredBackBufferWidth / 10000;
            actionScale = 7.7f * graphics.PreferredBackBufferWidth / 10000;
            panelScale = 4.7f * graphics.PreferredBackBufferWidth / 10000;

            resourcePosition = new Vector2(game.GraphicsDevice.Viewport.Width - 250, 0);
            panelRightPosition = new Vector2(game.GraphicsDevice.Viewport.Width - 300 * panelScale, game.GraphicsDevice.Viewport.Height - 300 * panelScale);
            panelLeftPosition = new Vector2(0, game.GraphicsDevice.Viewport.Height - 300 * panelScale);
            actionPosition = new Vector2(-100, game.GraphicsDevice.Viewport.Height - 430 * panelScale);
            timerBackgroundPosition = new Vector2(game.GraphicsDevice.Viewport.Width / 2 - 150 * timerScale, 0);
            timerPosition = new Vector2(game.GraphicsDevice.Viewport.Width / 2 - 150 * timerScale, 0);
            panelRightName = "Move Needed";
        }

        protected override void LoadContent()
        {
            font = Game.Content.Load<SpriteFont>(@"Fonts\description");
            timerFont = Game.Content.Load<SpriteFont>(@"Fonts\timer"); 

            resources = Game.Content.Load<Texture2D>("Textures/Level/resources");
            panelRight = Game.Content.Load<Texture2D>("Textures/Level/panelRight");
            panelLeft = Game.Content.Load<Texture2D>("Textures/Level/panelLeft");
            actionBackground = Game.Content.Load<Texture2D>("Textures/Level/actionBackground");
            timerBackgroundNetural = Game.Content.Load<Texture2D>("Textures/Level/timerBackground");
            timerBackgroundOn = Game.Content.Load<Texture2D>("Textures/Level/timerBackgroundOn");
            timerBackgroundOff = Game.Content.Load<Texture2D>("Textures/Level/timerBackgroundOff");

            timerRectangle = new Rectangle((int)timerBackgroundPosition.X, (int)timerBackgroundPosition.Y,
                (int)(timerBackgroundNetural.Width * timerScale), (int)(timerBackgroundNetural.Height * timerScale));

            base.LoadContent();
        }

        public override void Initialize()                                                                   //init
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)                                                      //update
        {
            fadeHelper += (float)gameTime.ElapsedGameTime.Milliseconds / 10;

            if (fadeHelper >= 1.0f)
            {
                fadeHelper = 0;
                fadeText--;
                if (fadeText <= 0)
                {
                    action = false;
                    fadeText = 255;
                    actionPosition = new Vector2(-100, Game.GraphicsDevice.Viewport.Height - 430 * panelScale);
                }
            }

            base.Update(gameTime);
        }

        public void ActionText(String text)
        {
            action = true;
            actionText = text;
        }

        public override void Draw(GameTime gameTime)
        {
            Player player = (Player)Game.Services.GetService(typeof(Player));
            Level level = (Level)Game.Services.GetService(typeof(Level));

            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.Draw(resources, resourcePosition, null, Color.White, 0f, Vector2.Zero,
                resourcesScale, SpriteEffects.None, 0.1f);

            if (player.turn)
            {
                if (timerRectangle.Intersects(new Rectangle(Input.MouseState.X, Input.MouseState.Y, 1, 1)))
                {
                    if (Input.MouseState.LeftButton == ButtonState.Pressed)
                    {
                        player.turn = false;
                        //player.SetSynchronize();
                    }
                    timerBackground = timerBackgroundOn;
                }
                else
                {
                    timerBackground = timerBackgroundNetural;
                }

                timerText = player.TimeLeft.Minutes.ToString("d2") + " : " + player.TimeLeft.Seconds.ToString("d2");
            }
            else
            {
                if (timerRectangle.Intersects(new Rectangle(Input.MouseState.X, Input.MouseState.Y, 1, 1)))
                {
                    if (Input.MouseState.LeftButton == ButtonState.Pressed)
                    {
                        player.TimeLeft = TimeSpan.Zero;
                        player.turn = true;
                        //player.Synchronize();
                    }
                    timerBackground = timerBackgroundOn;
                }
                else
                {
                    timerBackground = timerBackgroundOff;
                    timerText = "Not your Turn";
                }
            }

            OurGame.spriteBatch.Draw(timerBackground, timerBackgroundPosition, null, Color.White, 0f, Vector2.Zero,
                resourcesScale, SpriteEffects.None, 0.1f);

            OurGame.spriteBatch.DrawString(timerFont, timerText, 
                new Vector2(timerPosition.X + (170 - timerText.Length * 10) * timerScale , timerPosition.Y + 15 * timerScale), Color.White,
                0f, Vector2.Zero, timerScale * 1.6f, SpriteEffects.None, 0f);

            OurGame.spriteBatch.Draw(panelRight, panelRightPosition, null, Color.White, 0f, Vector2.Zero,
                 panelScale, SpriteEffects.None, 0.1f);

            OurGame.spriteBatch.Draw(panelLeft, panelLeftPosition, null, Color.White, 0f, Vector2.Zero,
                panelScale, SpriteEffects.None, 0.1f);

            if (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].visible)
            {
                if (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship == null)
                {
                    if (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].oilSpill)
                    {
                        panelLeftName = "OilSpill";
                        panelLeftText = "Can't move here";
                    }
                    else
                    {
                        panelLeftName = level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].name;
                        panelLeftText =
                           "Oil:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].oil.ToString() + "\n\n" +
                           "DefenseBonus:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].defenseBonus.ToString() + "\n" +
                           "OffenseBonus:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].offenseBonus.ToString();
                    }
                }
                else
                {
                    panelLeftName = level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.name;
                    panelLeftText =
                        "Health:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.health.ToString() + "\n" +
                        "Defense:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.defense.ToString() + "\n" +
                        "Offense:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.offense.ToString() + "\n" +
                        "Oil:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.oil.ToString() + "\n" +
                        "Move/Turn:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.movement.ToString() + "\n" +
                        "Consumption:  " + level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.consumption.ToString();
                }
            }
            else
            {
                panelLeftName = "Mist";
                panelLeftText = "Can't see what's there";
            }

            OurGame.spriteBatch.DrawString(font, panelLeftName, new Vector2(30, Game.GraphicsDevice.Viewport.Height - 285 * panelScale), Color.White,
                0f, Vector2.Zero, panelScale * 1.4f, SpriteEffects.None, 0f);
            OurGame.spriteBatch.DrawString(font, panelLeftText, new Vector2(20, Game.GraphicsDevice.Viewport.Height - 230 * panelScale), Color.White,
                0f, Vector2.Zero, panelScale * 1.3f, SpriteEffects.None, 0f);

            if (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship != null)
            {
                if (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.path != null)
                    panelRightText = (level.hexagonCollection[(int)level.selected.X, (int)level.selected.Y].ship.path.Count - 1).ToString();
                else
                {
                    panelRightText = "";
                }
            }
            else
            {
                panelRightText = "";
            }

            OurGame.spriteBatch.DrawString(font, panelRightName, new Vector2(Game.GraphicsDevice.Viewport.Width - 180 * panelScale,
                Game.GraphicsDevice.Viewport.Height - 285 * panelScale), Color.White,
                0f, Vector2.Zero, panelScale * 1.3f, SpriteEffects.None, 0f);
            OurGame.spriteBatch.DrawString(font, panelRightText, new Vector2(Game.GraphicsDevice.Viewport.Width - 200 * panelScale,
                Game.GraphicsDevice.Viewport.Height - 230 * panelScale), Color.White,
                0f, Vector2.Zero, panelScale * 1.3f, SpriteEffects.None, 0f);

            if(action)
            {
                if (actionPosition.X < 0)
                    actionPosition = new Vector2(actionPosition.X + 7.1f * actionScale, actionPosition.Y);
                OurGame.spriteBatch.Draw(actionBackground, actionPosition, null, new Color((byte)255, (byte)255, (byte)255, (byte)(fadeText))
                    , 0f, Vector2.Zero, actionScale, SpriteEffects.None, 0.1f);

                OurGame.spriteBatch.DrawString(font, actionText, new Vector2(actionPosition.X + 3.8f * actionScale, actionPosition.Y + 40f * actionScale)
                    , new Color((byte)255, (byte)255, (byte)255, (byte)(fadeText)), 0f, Vector2.Zero, actionScale * 0.7f, SpriteEffects.None, 0f);
            }

            OurGame.spriteBatch.DrawString(font, player.oil.ToString(),
                new Vector2(resourcePosition.X + 51.5f * resourcesScale,
                    resourcePosition.Y + (resources.Height * resourcesScale / 3)), Color.White,
                    0f, Vector2.Zero, resourcesScale * 1.8f, SpriteEffects.None, 0f);

            OurGame.spriteBatch.DrawString(font, player.money.ToString(),
                new Vector2(resourcePosition.X + 200f * resourcesScale,
                    resourcePosition.Y + (resources.Height * resourcesScale / 3)), Color.White,
                    0f, Vector2.Zero, resourcesScale * 1.8f, SpriteEffects.None, 0f);

            OurGame.spriteBatch.End();

            base.Draw(gameTime);                                                                            //draw out the menubar
        }
    }
}